package wang.z.utils;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @author like
 * @date 2022/12/3 23:39
 * @Description 于判断字符串是否为空、获取当前时间、对时间进行格式化以及获取当前登录用户等。
 */
public class toolUtil {
    public static boolean isEmpty(String str) {
        if (str != null && !"".equals(str.trim())) {
            return false;
        }
        return true;
    }

    public static Long getTime() {
        long time = System.currentTimeMillis();
        return time;
    }

    public static String getDateByTime(ArrayList<Integer> time) {
        String string="";
        if (time == null) {
            return "尚未还书";
        }
        if(time.size()==6) {
            string = minString(time.get(0)) + "-" + minString(time.get(1)) + "-" + minString(time.get(2)) + " "
                    + minString(time.get(3)) + ":" + minString(time.get(4)) + ":" + minString(time.get(5));
        }else {
            string = minString(time.get(0)) + "-" + minString(time.get(1)) + "-" + minString(time.get(2)) + " "
                    + minString(time.get(3)) + ":" + minString(time.get(4)) + ":" + "00";
        }

        return string;
    }
    public static String minString(Integer s){

        String string = "";
        if(s<10){
            string += "0"+s.toString();
        }else {
            string += s.toString();
        }
        return string;
    }
    public static boolean isPhoneLegal(String str)throws PatternSyntaxException {
        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
    }

    /**
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
     * 此方法中前三位格式有：
     * 13+任意数
     * 15+除4的任意数
     * 18+除1和4的任意数
     * 17+除9的任意数
     * 147
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 香港手机号码8位数，5|6|8|9开头+7位任意数
     */
    public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {
        String regExp = "^(5|6|8|9)\\d{7}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

}



